package fr.umlv.ig.MetroView;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import fr.umlv.ig.model.Line;
import fr.umlv.ig.model.MetroModel;
import fr.umlv.ig.model.Station;

public class MetroMapCases {

	
	/************* init keys of mapCases map ***********************/
	public static void initKeys(Map<Point, List<Station>> mapCases, int caseWidth, int caseHeight) {
		for(int i=0; i<10; i++) {
			for(int j=0; j<10; j++){
				Point p = new Point(i*caseWidth,j*caseHeight);
				mapCases.put(p, new ArrayList<Station>());
			}
		}	
	}
	
	public static void initValues(MetroModel metroModel, Map<Point, List<Station>> mapCases
			, int stationSize, int caseWidth, int caseHeight) {
		int numberOfLine = metroModel.getLineCount();
		for(int i = 0; i<numberOfLine; i++) {
			Line line = metroModel.getLine(i);
			int numberOfItinerary = line.getCountItinerary();
			Set<String> stationsDraw = new HashSet<String>();
			for(int j=0; j < numberOfItinerary; j++) {
				nextStation( mapCases, line.getItinerary(j).getStart(), stationsDraw, stationSize, caseWidth, caseHeight);
			}
		}
	}
	
	private static void nextStation(Map<Point, List<Station>> mapCases, Station station, Set<String> stationsDraw,
			int stationSize, int caseWidth, int caseHeight) {
		stationsDraw.add(station.getName());
		int stationHalfSize = stationSize/2;
		Map<Station,Station> mapNextStations = station.getMapNextStations();
		for(Entry<Station, Station> entryStation : mapNextStations.entrySet()) {
			Station nextStation = entryStation.getValue();
			if(!stationsDraw.contains(nextStation.getName())) {
				nextStation(mapCases, nextStation, stationsDraw, stationSize, caseWidth, caseHeight);
			}	
		}	
		int x = station.getX() - stationHalfSize;
		int y = station.getY() - stationHalfSize;
		Rectangle rectStation = new Rectangle(x , y, stationSize , stationSize);
		
		Rectangle rectCase = new Rectangle(new Dimension(caseWidth, caseHeight));
		for(Point p : mapCases.keySet()) {
			rectCase.setLocation(p);
			if(rectCase.intersects(rectStation)) {
				List<Station> listCasesStations = mapCases.get(p);
				listCasesStations.add(station);
			}
		}
	}
	
	/**********with the point in parameter, get the point of the case *******/
	public Point getCase(Point p, int caseWidth, int caseHeight) {
		Point newPoint = new Point();
		newPoint.x = p.x - (p.x%caseWidth);
		newPoint.y = p.y - (p.y%caseHeight);
		return newPoint;
	}

	public Point getCase(int x, int y, int caseWidth, int caseHeight) {
		Point newPoint = new Point();
		newPoint.x = x - (x%caseWidth);
		newPoint.y = y - (y%caseHeight);
		return newPoint;
	}
	
//	public void onMouseClicked(MouseEvent e) {
//		Point casePoint = getCase(e.getPoint());
//		//System.out.println("position clicked : " + e.getPoint());
//		//System.out.println("position case : " + casePoint);
//		List<Station> listStations = mapCases.get(casePoint);
//		
//		Graphics2D gs = mapImage.createGraphics();
//		//draw the top left corner of the case
//		gs.drawOval(casePoint.x, casePoint.y, 5, 5);
//		for(Station s : listStations) {
//			//System.out.println("position station : " + s.getCoordinate());
//			int x = (s.getX()-topLeftCornerX)*scale - stationHalfSize*scale;
//			int y = (s.getY()-topLeftCornerY)*scale - stationHalfSize*scale;
//			Rectangle rectStation = new Rectangle(x , y, stationSize*scale , stationSize*scale );
//			//System.out.println("position rectStation" + rectStation);
//			if(rectStation.contains(e.getPoint())){
//				System.out.println("YOU HAVE CLICKED IN A STATION : " + s.toString());
//				gs.drawString(s.getName(), x, y);
//				gs.setColor(Color.RED);
//			}
//			else {
//				gs.setColor(Color.WHITE);
//			}
//			gs.draw(rectStation);
//		}
//	}
}
